{% if cart.item_count != 0 %}
<form class="minicart_form" action="/cart" method="post" novalidate="">
    <div class="minicart_list">
        <ul>
            {% for list in cart.items %}
            <li class="cart_list_content">
                <div class="product_info">
                    <a class="product_info_pic" href="/products/{{ list.product.handle }}?data_from={{data_from}}"><img src="{{ list.image | image_url:'160x160' }}"></a>
                    <div class="product_info_info">
                        <a class="product_info_name" style="display: block;" href="/products/{{ list.product.handle }}?data_from={{data_from}}">{{ list.product.title }}</a>
                        {% unless list.product.has_only_default_variant %}
                        <div class="product_info_sku">
                            {% for option in list.options_with_values %}
                            {% if forloop.last == true %}
                            {{ option.name }}: {{ option.value }}
                            {% else %}
                            {{ option.name }}: {{ option.value }}, 
                            {% endif %}                             
                            {% endfor %}
                        </div>
                        {% endunless %}
                        <div class="cart_list_content_items">
                        	<div class="cart_qty_content">
	                            <div class="minicart_qty" inventory_policy="{{ list.product.inventory_policy }}" inventory_tracking="{{ list.product.inventory_tracking }}" inventory_quantity="{{ list.variant.inventory_quantity }}">
	                                <a class="minicart_qty_minus" href="javascript:void(0);">{% include icon_minus ,{width:'16',height:'16'} %}</a>
	                                <input class="minicart_qty_txt" type="text" name="minicart_qty" value="{{ list.quantity }}">
	                                <a class="minicart_qty_plus" href="javascript:void(0);">{% include icon_plus ,{width:'16',height:'16'} %}</a>
	                            </div>
	                            {% if list.product.inventory_tracking %}
								{% if list.quantity > list.variant.inventory_quantity %}
								<div class="tip">{{ lang.general.stock }}: {{ list.variant.inventory_quantity }}</div>
								{% endif %}
								{% endif %}
	                        </div>
                        </div>
                        <div class="cart_list_content_items product_total">{{ list.unit_price | money }}</div>
                    </div>
                </div>
            </li>
            {% endfor %}
        </ul>
    </div>
    <div class="minicart_bom">
        <div class="minicart_price" style="margin-bottom:14px">
            <ul>                
                <li>
                    <span>{{ lang.cart.list.subtotal }}</span>
                    <span id="minicart_total_price">{{ cart.items_subtotal_price | money }}</span>
                </li>
            </ul>
        </div>
        <div class="minicart_order_btn">
            {% if cart.has_out_of_stock %}
			<button class="minicart_checkout main_btn checkout_out_of_stock" type='submit' href="javascript:void(0);">{{ lang.cart.list.checkout }}</button>
			{% else %}
			<button class="minicart_checkout main_btn" type='submit' id="minicart_checkout" href="javascript:void(0);">{{ lang.cart.list.checkout }}</button>
			
                        {% if payment_form_html %}
                        <div class="minicart-payment" style="margin-top:14px" {% if cart.has_out_of_stock %}style="display:none;"{% endif %}>{{ payment_form_html }}</div>
                        {% endif %}
			{% endif %}


        </div>
        <div class="minicart_order_link" style="margin-top:14px"><a class="links" href="/cart">{{ lang.cart.list.view_cart }}</a></div>
    </div>
</form>


<script type="text/javascript">
	console.log({{cart|json}})
	$(function(){
		qty();
		console.log('执行代码')
	
		var _h1 = $('.minicart_top').innerHeight();
		var _h2 = $('.minicart_bom').innerHeight();
		var _h = _h1 + _h2 + 2;
		$('.minicart_list').css('max-height','calc(100vh - '+ _h +'px)');
		$('.minicart .close_btn,.minicart').click(function(){
			$('.minicart').removeClass('minicart_on');
		});
		$('.minicart_wrapper').click(function(e){
			e.stopPropagation();
		})
	})
	function qty(){
		var minus = $('.minicart_qty_minus');
		var plus = $('.minicart_qty_plus');
		var qtyText = $('.minicart_qty_txt');
		
		minus.click(function(){
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			var qtyVal = _this.siblings('.minicart_qty_txt').attr('value');
			if(qtyVal == ''){
				qtyVal = 1;
			}
			var inventory_policy = _this.parent().attr('inventory_policy');
			var inventory_tracking = _this.parent().attr('inventory_tracking');
			var inventory_quantity = _this.parent().attr('inventory_quantity');
			var newQtyVal;
			if(qtyVal >= 2 && qtyVal <= 999999){
				newQtyVal = parseInt(qtyVal) - 1;
				_this.siblings('.minicart_qty_txt').attr('value',newQtyVal);
			}else if(parseInt(qtyVal) > 999999){
				newQtyVal = 999999;
				_this.siblings('.minicart_qty_txt').attr('value','999999');
			}else{
				return false;
			}
			if(inventory_tracking == '1'){
				if(parseInt(newQtyVal) > parseInt(inventory_quantity)){
					if(_this.parent().siblings('.tip').length == 0){
						_this.parent().after('<div class="tip">{{ lang.general.stock }}: '+ inventory_quantity +'</div>');
					}
				}else{
					_this.parent().siblings('.tip').remove();
				}
			}
			if(qtyVal > 1){
				moi.ajax({
					url:'/cart/change',
					type: 'POST',
					contentType: "application/json",
					data:JSON.stringify({'line':line,'quantity':newQtyVal}),
	                dataType: 'json',
					success: function(res){
						var price = res.data.items_subtotal_price;
						$('#minicart_total_price').html(formatMoney(price, oemcart_currency));
						addCartNum();
						if(res.data.has_out_of_stock){
							$('.minicart_checkout').removeAttr('id');
							$('.minicart_checkout').addClass('checkout_out_of_stock');
						}else{
							$('.minicart_checkout').attr('id','minicart_checkout');
							$('.minicart_checkout').removeClass('checkout_out_of_stock');
						}
					},
					beforeSend: function(){
						createAjaxLoading()
			        },
			        complete: function(){
			            removeAjaxLoading()
			        }
				})
			}
		})





		plus.click(function(){
			
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			var qtyVal = _this.siblings('.minicart_qty_txt').attr('value');
			if(qtyVal == ''){
				qtyVal = 1;
			}
			var inventory_policy = _this.parent().attr('inventory_policy');
			var inventory_tracking = _this.parent().attr('inventory_tracking');
			var inventory_quantity = _this.parent().attr('inventory_quantity');
			var newQtyVal;

			if(qtyVal >= 0 && qtyVal <= 999999){
				newQtyVal = parseInt(qtyVal) + 1;
				_this.siblings('.minicart_qty_txt').attr('value',newQtyVal);
			}else if(parseInt(qtyVal) > 999999){
				newQtyVal = 999999;
			}
			if(inventory_tracking == '1'){
				if(parseInt(newQtyVal) > parseInt(inventory_quantity)){
					if(_this.parent().siblings('.tip').length == 0){
						_this.parent().after('<div class="tip">{{ lang.general.stock }}: '+ inventory_quantity +'</div>');
					}
				}else{
					_this.parent().siblings('.tip').remove();
				}
			}

			moi.ajax({
				url:'/cart/change',
				type: 'POST',
				contentType: "application/json",
				data:JSON.stringify({'line':line,'quantity':newQtyVal}),
                dataType: 'json',
				success: function(res){

					var price = res.data.items_subtotal_price;
					$('#minicart_total_price').html(formatMoney(price, oemcart_currency));
					addCartNum();
					if(res.data.has_out_of_stock){
						$('.minicart_checkout').removeAttr('id');
						$('.minicart_checkout').addClass('checkout_out_of_stock');
					}else{
						$('.minicart_checkout').attr('id','minicart_checkout');
						$('.minicart_checkout').removeClass('checkout_out_of_stock');
					}
				},
				beforeSend: function(){
					createAjaxLoading()
		        },
		        complete: function(){
		            removeAjaxLoading()
		        }
			})
		})
		qtyText.keyup(function(){
			$(this)[0].value = $(this)[0].value.replace(/^(0+)|[^\d]+/g,'');
			if($(this)[0].value > 999999){
				$(this)[0].value = 999999;
			}
		})
		qtyText.blur(function(){
			if($(this).attr('value') == ""){
				$(this).attr('value','1');
			}
		})
		qtyText.change(function(){
			var _this = $(this);
			var _index = _this.parents('.cart_list_content').index();
			var line = parseInt(_index) + 1;
			var qtyVal = _this.attr('value');
			if(qtyVal == ''){
				qtyVal = 1;
			}
			var inventory_policy = _this.parent().attr('inventory_policy');
			var inventory_tracking = _this.parent().attr('inventory_tracking');
			var inventory_quantity = _this.parent().attr('inventory_quantity');
			var newQtyVal;
			if(parseInt(qtyVal) >= 0 && qtyVal <= 999999){
				newQtyVal = parseInt(qtyVal);
				_this.siblings('.minicart_qty_txt').attr('value',newQtyVal);
			}else if(parseInt(qtyVal) > 999999){
				newQtyVal = 999999;
			}
			if(inventory_tracking == '1'){
				if(parseInt(newQtyVal) > parseInt(inventory_quantity)){
					qtyVal = inventory_quantity;
					if(_this.parent().siblings('.tip').length == 0){
						_this.parent().after('<div class="tip">{{ lang.general.stock }}: '+ inventory_quantity +'</div>');
					}
				}else{
					_this.parent().siblings('.tip').remove();
				}
			}

			moi.ajax({
				url:'/cart/change',
				type: 'POST',
				contentType: "application/json",
				data:JSON.stringify({'line':line,'quantity':newQtyVal}),
                dataType: 'json',
				success: function(res){
					
					var price = res.data.items_subtotal_price;
					$('#minicart_total_price').html(formatMoney(price, oemcart_currency));
					addCartNum();
					if(res.data.has_out_of_stock){
						$('.minicart_checkout').removeAttr('id');
						$('.minicart_checkout').addClass('checkout_out_of_stock');
					}else{
						$('.minicart_checkout').attr('id','minicart_checkout');
						$('.minicart_checkout').removeClass('checkout_out_of_stock');
					}
				},
				beforeSend: function(){
					createAjaxLoading()
		        },
		        complete: function(){
		            removeAjaxLoading()
		        }
			})
		})
		
		
		


	}


</script>
{% else %}
<div class="minicart_empty_content">
	<div class="cart_empty_svg">{% include icon_cart ,{width:'80',height:'80'} %}</div>
	<p>{{ lang.cart.list.your_shopping_cart_is_empty }}</p>
</div>
<script type="text/javascript">
$(function(){
	$('.minicart .close_btn,.minicart').click(function(){
			$('.minicart').removeClass('minicart_on');
		});
		$('.minicart_wrapper').click(function(e){
			e.stopPropagation();
		})
})
</script>
{% endif %}